<template>
  <div class="app-container">
    <splitpanes :horizontal="appStore.device === 'mobile'" class="default-theme">
      <pane size="16">
        <el-tree
          class="custom-tree"
          :data="treeData"
          :props="defaultProps"
          default-expand-all
          highlight-current
          @node-click="handleNodeClick" />
      </pane>
      <pane size="84">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="90px">
          <el-form-item label="语料名称" prop="partyName">
            <el-input
              v-model="queryParams.partyName"
              placeholder="请输入语料名称"
              clearable
              @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb10">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
          </el-col>
          <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['partyinfo:partyinfo:edit']">
          修改
        </el-button>
      </el-col> -->
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete">删除</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" border :data="partyinfoList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="语料类型" align="center" prop="type">
            <template #default="scope">
              {{ photo_type.find(item => item.value === scope.row.type)?.label || '-' }}
            </template>
          </el-table-column>
          <el-table-column label="语料名称" align="center" prop="collectionLocation" />
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
            width="200px"
            fixed="right">
            <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
              <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList" />
      </pane>
    </splitpanes>

    <!-- 添加或修改语料信息对话框 -->
    <detail v-if="open" v-model:value="open" :title="title" :id="formId" @getList="getList" />
  </div>
</template>

<script setup name="Partyinfo">
import { listPhotoregistration, delPhotoregistration } from '@/api/photoRegistration/index.js'
import detail from './detail.vue'
import { Splitpanes, Pane } from 'splitpanes'
import 'splitpanes/dist/splitpanes.css'
import useAppStore from '@/store/modules/app'

const appStore = useAppStore()
const { proxy } = getCurrentInstance()
const { photo_type, collection_method } = proxy.useDict('photo_type', 'collection_method')

const partyinfoList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const formId = ref('')
const title = ref('')
const daterangeCreateTime = ref([])

const treeData = ref([
  {
    label: '全部语料',
    id: 0,
  },
  {
    label: '类型一',
    id: 1,
  },
  {
    label: '类型二',
    id: 2,
  },
])
const defaultProps = {
  children: 'children',
  label: 'label',
}

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    partyName: null,
    createTime: null,
  },
})

const { queryParams } = toRefs(data)

// 点击左侧树
const handleNodeClick = val => {
  console.log(val)
}
/** 查询语料信息列表 */
function getList() {
  loading.value = true
  queryParams.value.params = {}
  if (null != daterangeCreateTime.value && '' != daterangeCreateTime.value) {
    queryParams.value.params['beginCreateTime'] = daterangeCreateTime.value[0]
    queryParams.value.params['endCreateTime'] = daterangeCreateTime.value[1]
  }
  listPhotoregistration(queryParams.value).then(response => {
    partyinfoList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeCreateTime.value = []
  proxy.resetForm('queryRef')
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  formId.value = undefined
  open.value = true
  title.value = '添加语料信息'
}

/** 修改按钮操作 */
function handleUpdate(row) {
  formId.value = row.id || ids.value
  open.value = true
  title.value = '修改语料信息'
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal
    .confirm('是否确定删除语料信息编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delPhotoregistration(_ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

getList()
</script>

<style lang="scss" scoped></style>
